import java.util.Arrays;

public class demo1 {
    public static void main(String[] args) {
             int[] num = {1, 2, 3, 4, 5};
                rotate(num, 2);
                System.out.println(Arrays.toString(num));
            }



   public static void rotate(int[] num, int k){
                    k%=num.length;//取余，防止k超过数组长度
                    reverse(num,0,num.length-1);//需-1，否则越界
                    reverse(num,0,k-1);
                    reverse(num,k,num.length-1);

                }
                public static void reverse(int[]num,int start,int end){//分块交换
                    while(start<end){
                        int tmp=num[start];
                        num[start]=num[end];
                        num[end]=tmp;
                        start+=1;
                        end-=1;
                    }
                }

            }





